<?php
class Video_model extends Model
{
	function Video_model()
	{
	parent::Model();
	}
	function addcategeory()
	{
		$this->db->insert('videos_categeory',array('name'=>$_POST['name']));
	}
	function getvideocategeories()
	{
		//$this->db
		$query = $this->db->get('videos_categeory');
		return $query->result();
	}
	function addvideo()
	{		
		$this->db->insert('videos' , array('name'=> $_POST['name'],'no_of_views' => $_POST['no_of_views'],
		                  'video_cat_id' => $_POST['video_cat_id'],'time' =>$_POST['time']));
		$id= $this->db->insert_id();
		return $id;
	}
	function uploadvideo($i)
	{
		//uploading video
		$this->load->library('upload');
		$dir =  './assets/videos';   
	    $config['upload_path'] = $dir;
		$config['allowed_types'] = 'flv';
		$config['max_size']	= '90000000';
    	//$config['max_width']  = '1024';
    	//$config['max_height']  = '768';
		
		$this->upload->initialize($config);
	
		if (!$this->upload->do_upload('video'))
		{
			$error = array('error' => $this->upload->display_errors());
			print_r($error);
			
			echo  "hello";
			//$this->load->view('admin_images', $error);
		}	
		else
		{
		   $data = array('upload_data' => $this->upload->data());
	       $filepath = $data['upload_data']['file_name'];
	       rename($dir.'/'.$filepath , $dir.'/video'.$i.'.flv');
	       $this->db->where('id',$i);
	       $this->db->update('videos',array('videouploaded'=>'1'));	
		
		//uploading image
		$dir1 =  './assets/videos/image_preview';   
	    $config1['upload_path'] = $dir1;
		$config1['allowed_types'] = 'gif|jpg|png|bmp';
		$config1['max_size']	= '90000000';
    	//$config['max_width']  = '1024';
    	//$config['max_height']  = '768';
		
		$this->upload->initialize($config1);
	
		if (!$this->upload->do_upload('image'))
		{
			$error = array('error' => $this->upload->display_errors());
			print_r($error);
			
			//$this->load->view('admin_images', $error);
		}	
		else
		{
		   $data = array('upload_data' => $this->upload->data());
	       $filepath = $data['upload_data']['file_name'];
	       rename($dir1.'/'.$filepath , $dir1.'/image'.$i.'.jpg');	

           if (!$this->upload->do_upload('thumbimage'))
		  {
			$error = array('error' => $this->upload->display_errors());
			echo "hai";
			print_r($error);
			
			//$this->load->view('admin_images', $error);
		  }	
		  else
		  {
		   $data = array('upload_data' => $this->upload->data());
	       $filepath = $data['upload_data']['file_name'];
	       rename($dir1.'/'.$filepath , $dir1.'/image'.$i.'_home.jpg');	
		  }
		}
	       
//		$this->load->library('image_lib');
//    	$filename = 'image'.$i.'.jpg';
//    	
//    	$config3['image_library'] = 'gd2';
//        $config3['source_image'] = $dir1.'/'.$filename;
//        $config3['new_image'] = 'home_image'.$i.'.jpg';
//      	$config3['create_thumb'] = TRUE;
//		$config3['maintain_ratio'] = TRUE;
//		$config3['width'] = 80;
//		$config3['height'] = 65;
//    	
//    	      
//    	$this->image_lib->initialize($config3);
//    	
//    	if(!$this->image_lib->resize())
//    	{
//    		$error = array('error' => $this->image_lib->display_errors());
//			print_r($error);
//    	}
//    	$this->image_lib->clear();
	       
		//generating image to display in recent list
		$this->load->library('image_lib');
    	$filename = 'image'.$i.'.jpg';
    	
    	$config2['image_library'] = 'gd2';
        $config2['source_image'] = $dir1.'/'.$filename;
      	$config2['create_thumb'] = TRUE;
		$config2['maintain_ratio'] = TRUE;
		$config2['width'] = 150;
		$config2['height'] = 110;
    	
    	      
    	$this->image_lib->initialize($config2);
    	
    	if(!$this->image_lib->resize())
    	{
    		$error = array('error' => $this->image_lib->display_errors());
			print_r($error);
    	}
		}
	 }
	function crop()
	{
		$this->load->helper('imagecrop');

		$upload_dir = "./uploads"; 				// The directory for the images to be saved in
		$upload_path = $upload_dir."/";				// The path to where the image will be saved
		//$large_image_prefix = "resize_"; 			// The prefix name to large image
		$thumb_image_prefix = "thumbnail_";			// The prefix name to the thumb image
		$large_image_name = $_POST['main_image'];     // New name of the large image (append the timestamp to the filename)
		$thumb_image_name = $thumb_image_prefix.$_POST['main_image_id'];     // New name of the thumbnail image (append the timestamp to the filename)
		$thumb_width = "94";						// Width of thumbnail image
		$thumb_height = "110";						// Height of thumbnail image

		$large_image_location = $large_image_name;
		$thumb_image_location = $upload_path.$thumb_image_name.'.jpg';
		
		 	
			$x1 = $_POST["x1"];
			$y1 = $_POST["y1"];
			$x2 = $_POST["x2"];
			$y2 = $_POST["y2"];
			$w = $_POST["w"];
			$h = $_POST["h"];
			//Scale the image to the thumb_width set above
			$scale = $thumb_width/$w;
			$cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
			echo $cropped;
	}
		
  
   function getcategeories()
   {
   	  $query = $this->db->get('videos_categeory');
   	  return $query;
   	  
   }
   function getvideos($id,$table)
   {
   	  $this->db->where('videouploaded',1);
      $this->db->where('video_cat_id',$id);
      $this->db->order_by('id','desc');
   	  $query = $this->db->get($table);
   	  return $query;
   }
   function deletecategeory($id)
   {   	
   	$removeid=array();
   	  if(isset($_POST['removeid'])){
   	  	$removeid = $_POST['removeid'];
   	  }
   	  else {
   	  	$removeid = array($id);
   	   	  }
   	   	   	   	
   	    foreach($removeid as $item){
   	   	    
   	   	  	$videos = array();
   	   	  	$videos = $this->getvideos($item,'videos');
   	   	  
   	   	   if($videos->num_rows()>0){
   	   	  	foreach($videos->result() as $video){
   	   	  	echo $video->id;		
   	   	  	$this->deletevideo($video->id);
   	   	  	}
   	   	  	
   	   	   }
   	   	    $this->db->where('id',$item);
   	     $this->db->delete('videos_categeory');
   	   	  }
   	    
   	   }
      
 
   function getcategeorydetails($id)
   {
   	  $this->db->where('id',$id);
   	  $query = $this->db->get('videos_categeory');
   	  return $query->result_array();
   }
   function editcategeory()
   {
   	 $this->db->where('id',$_POST['id']);
   	 $this->db->update('videos_categeory',array('name' => $_POST['name']));
   	
   }
   function getvideodetails($id)
   {
   	$this->db->where('id',$id);
   	$query = $this->db->get('videos');
   	return $query->result_array();
   	   	
   }
   function deletevideo($id)
   {
   	if(isset($_POST['removeid1'])){
   		$removeid = $_POST['removeid1'];
   		$keys_to_delete = '';
    	   foreach($removeid as $item){
   	   	
   	   	  $keys_to_delete.=  "'$item[id]',";
   	   	  unlink('./assets/videos/video'.$item[id].'.flv');
   	 	  unlink('./assets/videos/image_preview/image'.$item[id].'.jpg');
   	 	  unlink('./assets/videos/image_preview/thumbnail'.$item[id].'.jpg');
   	  	  unlink('./assets/videos/image_preview/image'.$item[id].'_thumb.jpg');
   	   }
   	 if($keys_to_delete !='')
   	   {
   	   $keys_mod =  substr($keys_to_delete,0,strlen($keys_to_delete)-1);
   	   }
   	}
   	else
   	{
   		$keys_mod = "'$id'";
   		 unlink('./assets/videos/video'.$id.'.flv');
   	 	  unlink('./assets/videos/image_preview/image'.$id.'.jpg');
   	 	  unlink('./assets/videos/image_preview/thumbnail'.$id.'.jpg');
   	  	  unlink('./assets/videos/image_preview/image'.$id.'_thumb.jpg');
   	}
     	$this->db->query('delete from videos where id in ('.$keys_mod.')');	
     	
   	}
   function editvideo($id)
   {
   	 $this->db->where('id',$id);
   	 $this->db->update('videos',array('id' => $_POST['id'], 'name' => $_POST['name'], 'no_of_views' => $_POST['no_of_views']));
   }
}
?>